document.addEventListener('DOMContentLoaded', function() {
    // 公告悬浮窗功能
    const announcementModal = document.getElementById('announcementModal');
    const closeAnnouncementBtn = document.getElementById('closeAnnouncement');
    const learnMoreBtn = document.getElementById('learnMoreBtn');
    
    // 页面加载后显示公告
    setTimeout(function() {
        announcementModal.style.display = 'flex';
    }, 1000); // 延迟1秒显示
    
    // 关闭公告
    closeAnnouncementBtn.addEventListener('click', function() {
        announcementModal.style.display = 'none';
    });
    
    // 点击公告外部区域关闭
    announcementModal.addEventListener('click', function(e) {
        if (e.target === announcementModal) {
            announcementModal.style.display = 'none';
        }
    });
    
    // 了解更多按钮
    learnMoreBtn.addEventListener('click', function() {
        // 可以跳转到活动详情页或其他操作
        window.location.href = '#join';
        announcementModal.style.display = 'none';
    });
    
    // 汉堡菜单切换
    const hamburger = document.querySelector('.hamburger');
    const nav = document.querySelector('nav');
    
    hamburger.addEventListener('click', function() {
        hamburger.classList.toggle('active');
        nav.classList.toggle('active');
    });
    
    // 点击导航链接关闭菜单
    const navLinks = document.querySelectorAll('nav ul li a');
    navLinks.forEach(link => {
        link.addEventListener('click', function() {
            hamburger.classList.remove('active');
            nav.classList.remove('active');
        });
    });
    
    // 滚动时导航栏高亮
    window.addEventListener('scroll', function() {
        const sections = document.querySelectorAll('section');
        const navLinks = document.querySelectorAll('nav ul li a');
        
        let current = '';
        
        sections.forEach(section => {
            const sectionTop = section.offsetTop;
            const sectionHeight = section.clientHeight;
            
            if (pageYOffset >= sectionTop - 200) {
                current = section.getAttribute('id');
            }
        });
        
        navLinks.forEach(link => {
            link.classList.remove('active');
            if (link.getAttribute('href').substring(1) === current) {
                link.classList.add('active');
            }
        });
    });
    
    // 复制服务器地址
    const copyBtn = document.querySelector('.copy-btn');
    if (copyBtn) {
        copyBtn.addEventListener('click', function() {
            const serverAddress = this.getAttribute('data-clipboard-text');
            
            // 创建临时输入框
            const tempInput = document.createElement('input');
            tempInput.value = serverAddress;
            document.body.appendChild(tempInput);
            
            // 选择并复制
            tempInput.select();
            document.execCommand('copy');
            
            // 移除临时输入框
            document.body.removeChild(tempInput);
            
            // 更改按钮文本
            const originalText = this.textContent;
            this.textContent = '已复制!';
            
            // 恢复原始文本
            setTimeout(() => {
                this.textContent = originalText;
            }, 2000);
        });
    }
    
    // 表单提交处理
    const contactForm = document.getElementById('contactForm');
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单数据
            const name = document.getElementById('name').value;
            const email = document.getElementById('email').value;
            const message = document.getElementById('message').value;
            
            // 这里可以添加表单验证逻辑
            
            // 模拟表单提交
            alert(`感谢您的留言，${name}！我们会尽快回复您。`);
            
            // 重置表单
            contactForm.reset();
        });
    }
    
    // 图片加载动画
    const galleryItems = document.querySelectorAll('.gallery-item img');
    galleryItems.forEach(img => {
        img.addEventListener('load', function() {
            this.style.opacity = 1;
        });
    });
    
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            
            const targetId = this.getAttribute('href');
            const targetElement = document.querySelector(targetId);
            
            if (targetElement) {
                window.scrollTo({
                    top: targetElement.offsetTop - 80,
                    behavior: 'smooth'
                });
            }
        });
    });
    
    // 数字计数动画
    const stats = document.querySelectorAll('.stat .count');
    
    function animateCounter(el) {
        const target = parseInt(el.textContent);
        const suffix = el.textContent.match(/[+%]/) ? el.textContent.match(/[+%]/)[0] : '';
        let current = 0;
        const increment = target / 50;
        const timer = setInterval(() => {
            current += increment;
            el.textContent = Math.floor(current) + suffix;
            if (current >= target) {
                el.textContent = target + suffix;
                clearInterval(timer);
            }
        }, 30);
    }
    
    // 检查元素是否在视口中
    function isInViewport(el) {
        const rect = el.getBoundingClientRect();
        return (
            rect.top >= 0 &&
            rect.left >= 0 &&
            rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
            rect.right <= (window.innerWidth || document.documentElement.clientWidth)
        );
    }
    
    // 滚动时检查并触发动画
    let animated = false;
    window.addEventListener('scroll', function() {
        if (!animated && isInViewport(document.querySelector('.server-stats'))) {
            stats.forEach(animateCounter);
            animated = true;
        }
    });
    
    // 初始检查
    if (isInViewport(document.querySelector('.server-stats'))) {
        stats.forEach(animateCounter);
        animated = true;
    }
});